package com.lancoo.znbkxx.teacher.ui.adapter

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.activity.ComponentActivity
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.ClassLanguageAbilityInfor
import com.lancoo.znbkxx.teacher.utils.ButtonUtils
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import kotlinx.android.synthetic.main.aibk_adapter_item_english_ability.view.*

class AibkLanguageAbilityAdapter (
        val activity: ComponentActivity?,
        val classLanguageAbilityInfor: ClassLanguageAbilityInfor,
        val maxDistreNum:Int
): KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View? {
        return LayoutInflater.from(activity)
                .inflate(R.layout.aibk_adapter_item_english_ability, parent, false)
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {
        var conclusion = 0
        var goodStuNum = 0
        var generalStuNum = 0
        var badStuNum = 0
        if(position == 0){
            conclusion = classLanguageAbilityInfor.Hearing.Conclusion
            goodStuNum = classLanguageAbilityInfor.Hearing.GoodStuNum
            generalStuNum = classLanguageAbilityInfor.Hearing.GeneralStuNum
            badStuNum = classLanguageAbilityInfor.Hearing.BadStuNum

            holder.convertView.TvAbilityName.text = "听力能力"
        }else if(position == 1){
            conclusion = classLanguageAbilityInfor.Language.Conclusion
            goodStuNum = classLanguageAbilityInfor.Language.GoodStuNum
            generalStuNum = classLanguageAbilityInfor.Language.GeneralStuNum
            badStuNum = classLanguageAbilityInfor.Language.BadStuNum

            holder.convertView.TvAbilityName.text = "口语能力"
        }else if(position == 2){
            conclusion = classLanguageAbilityInfor.Reading.Conclusion
            goodStuNum = classLanguageAbilityInfor.Reading.GoodStuNum
            generalStuNum = classLanguageAbilityInfor.Reading.GeneralStuNum
            badStuNum = classLanguageAbilityInfor.Reading.BadStuNum

            holder.convertView.TvAbilityName.text = "阅读能力"
        }else if(position == 3){
            conclusion = classLanguageAbilityInfor.Writing.Conclusion
            goodStuNum = classLanguageAbilityInfor.Writing.GoodStuNum
            generalStuNum = classLanguageAbilityInfor.Writing.GeneralStuNum
            badStuNum = classLanguageAbilityInfor.Writing.BadStuNum

            holder.convertView.TvAbilityName.text = "写作能力"
        }

        if(conclusion == 0){
            holder.convertView.TvAssessment.visibility = View.GONE
            holder.convertView.IvEmptyData.visibility = View.VISIBLE
        }else{
            holder.convertView.TvAssessment.visibility = View.VISIBLE
            holder.convertView.IvEmptyData.visibility = View.GONE
            if(conclusion == 1){
                holder.convertView.TvAssessment.text = "整体较差"
                holder.convertView.TvAssessment.setTextColor(Color.parseColor("#f9ad4f"))
            }else if(conclusion == 2){
                holder.convertView.TvAssessment.text = "整体一般"
                holder.convertView.TvAssessment.setTextColor(Color.parseColor("#22d7bb"))
            }else if(conclusion == 3){
                holder.convertView.TvAssessment.text = "整体较好"
                holder.convertView.TvAssessment.setTextColor(Color.parseColor("#00afff"))
            }
        }
        showIvData(holder.convertView.IvGood, holder.convertView.IvGeneral, holder.convertView.IvBad,
                goodStuNum,generalStuNum,badStuNum)



        holder.convertView.RlAbailityView.setOnClickListener {
            if(conclusion == 0){
                return@setOnClickListener
            }
            if (!ButtonUtils.isFastDoubleClick(R.id.RlAbailityView)) {
                clickListener!!.onClick(position)
            }
        }
    }

    override fun getItemCount(): Int {
        return 4
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    private fun showIvData(IvGood:ImageView,
                           IvGeneral:ImageView,
                           IvBad:ImageView,
                           data1:Int,
                           data2:Int,
                           data3:Int){

        val para1 = IvGood.layoutParams
        para1.height = DensityUtils.dp2px(activity,data1*200f/maxDistreNum)
        IvGood.layoutParams = para1

        val para2 = IvGeneral.layoutParams
        para2.height = DensityUtils.dp2px(activity,(data1+data2)*200f/maxDistreNum)
        IvGeneral.layoutParams = para2

        val para3 = IvBad.layoutParams
        para3.height = DensityUtils.dp2px(activity,(data1+data2+data3)*200f/maxDistreNum)
        IvBad.layoutParams = para3
    }

    interface OnClickListener {
        fun onClick(pos:Int)
    }

    private var clickListener: OnClickListener? = null

    fun setClickListener(clickListener: OnClickListener?) {
        this.clickListener = clickListener
    }

}